package cn.bone.ou.myuser.controller;

import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.Ref;
import cn.bone.base.component.GlobalParam;
import cn.bone.ou.myuser.param.MyParam;
import cn.bone.ou.myuser.service.IMyService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Controller
@RequestMapping("/my")
public class MyUserController {

    @Resource
    private IMyService myService;
    @Resource
    private GlobalParam globalParam;


    @GetMapping("/login")
    public String loginPage(){
        return "/my/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public CommonResult login(String username, String password, HttpServletRequest request){

        GlobalParam.SuperMemberParam superMember = globalParam.getSuperMember();
        System.out.println(JSON.toJSONString(superMember));
        if(username.equalsIgnoreCase(superMember.getUsername())
        && password.equalsIgnoreCase(superMember.getPassword())){
            request.getSession().setAttribute("user",1);
            return Ref.ok();
        }
        return Ref.error("错误的用户名或密码");
    }

    @GetMapping("/list")
    public String listPage(){
        return "/my/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public CommonResult list(@Valid MyParam.ListParam param){

        return myService.list(param);
    }

    @RequestMapping("/regions")
    @ResponseBody
    public CommonResult regions(){
        return myService.regions();
    }

    @RequestMapping("/usable")
    @ResponseBody
    public CommonResult usable(@Valid MyParam.UsableParam param){
        return myService.usable(param);
    }

    @RequestMapping("/add")
    @ResponseBody
    public CommonResult add(@Valid MyParam.AddParam param){
        return myService.add(param);
    }

    @RequestMapping("/modify")
    @ResponseBody
    public CommonResult modify(@Valid MyParam.ModifyParam param){
        return myService.modify(param);
    }

    @RequestMapping("/listMng")
    @ResponseBody
    public CommonResult listMng(@Valid MyParam.ListMngParam param){
        return myService.listMng(param);
    }

    @RequestMapping("/usableMng")
    @ResponseBody
    public CommonResult usableMng(@Valid MyParam.UsableMngParam param){
        return myService.usableMng(param);
    }

    @RequestMapping("/deleteMng")
    @ResponseBody
    public CommonResult deleteMng(@Valid MyParam.DeleteMngParam param){
        return myService.deleteMng(param);
    }

    @RequestMapping("/resetPass")
    @ResponseBody
    public CommonResult resetPass(@Valid MyParam.ResetPassParam param){
        return myService.resetPass(param);
    }

    @RequestMapping("/addMng")
    @ResponseBody
    public CommonResult addMng(@Valid MyParam.AddMngParam param){
        return myService.addMng(param);
    }

    @RequestMapping("/modifyMng")
    @ResponseBody
    public CommonResult modifyMng(@Valid MyParam.ModifyMngParam param){
        return myService.modifyMng(param);
    }
}
